package com.luckcoder.relax.games.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.luckcoder.relax.games.domain.RelaxGameLeaderboard;
import com.luckcoder.relax.games.domain.bo.RelaxGameLeaderboardBo;
import com.luckcoder.relax.games.domain.vo.RelaxGameLeaderboardVo;
import com.luckcoder.relax.games.mapper.RelaxGameLeaderboardMapper;
import com.luckcoder.relax.games.service.IRelaxGameLeaderboardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 游戏排行榜Service业务层处理
 *
 * @author zhanghao
 * @date 2025-09-10
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class RelaxGameLeaderboardServiceImpl implements IRelaxGameLeaderboardService {
    
    private final RelaxGameLeaderboardMapper baseMapper;
    
    /**
     * 查询游戏排行榜
     *
     * @param rankId 主键
     * @return 游戏排行榜
     */
    @Override
    public RelaxGameLeaderboardVo queryById(Long rankId) {
        return baseMapper.selectVoById(rankId);
    }
    
    /**
     * 分页查询游戏排行榜列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 游戏排行榜分页列表
     */
    @Override
    public TableDataInfo<RelaxGameLeaderboardVo> queryPageList(RelaxGameLeaderboardBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<RelaxGameLeaderboard> lqw = buildQueryWrapper(bo);
        Page<RelaxGameLeaderboardVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }
    
    /**
     * 查询符合条件的游戏排行榜列表
     *
     * @param bo 查询条件
     * @return 游戏排行榜列表
     */
    @Override
    public List<RelaxGameLeaderboardVo> queryList(RelaxGameLeaderboardBo bo) {
        LambdaQueryWrapper<RelaxGameLeaderboard> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }
    
    private LambdaQueryWrapper<RelaxGameLeaderboard> buildQueryWrapper(RelaxGameLeaderboardBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<RelaxGameLeaderboard> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(RelaxGameLeaderboard::getRankId);
        lqw.eq(bo.getUserId() != null, RelaxGameLeaderboard::getUserId, bo.getUserId());
        lqw.eq(bo.getGameId() != null, RelaxGameLeaderboard::getGameId, bo.getGameId());
        lqw.eq(StringUtils.isNotBlank(bo.getGameCode()), RelaxGameLeaderboard::getGameCode, bo.getGameCode());
        lqw.eq(bo.getBestScore() != null, RelaxGameLeaderboard::getBestScore, bo.getBestScore());
        lqw.eq(bo.getTotalScore() != null, RelaxGameLeaderboard::getTotalScore, bo.getTotalScore());
        lqw.eq(bo.getPlayCount() != null, RelaxGameLeaderboard::getPlayCount, bo.getPlayCount());
        lqw.eq(bo.getTotalTime() != null, RelaxGameLeaderboard::getTotalTime, bo.getTotalTime());
        lqw.eq(bo.getAvgScore() != null, RelaxGameLeaderboard::getAvgScore, bo.getAvgScore());
        lqw.eq(StringUtils.isNotBlank(bo.getRankType()), RelaxGameLeaderboard::getRankType, bo.getRankType());
        lqw.eq(StringUtils.isNotBlank(bo.getRankPeriod()), RelaxGameLeaderboard::getRankPeriod, bo.getRankPeriod());
        lqw.eq(bo.getLastPlayTime() != null, RelaxGameLeaderboard::getLastPlayTime, bo.getLastPlayTime());
        return lqw;
    }
    
    /**
     * 新增游戏排行榜
     *
     * @param bo 游戏排行榜
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(RelaxGameLeaderboardBo bo) {
        RelaxGameLeaderboard add = MapstructUtils.convert(bo, RelaxGameLeaderboard.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setRankId(add.getRankId());
        }
        return flag;
    }
    
    /**
     * 修改游戏排行榜
     *
     * @param bo 游戏排行榜
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(RelaxGameLeaderboardBo bo) {
        RelaxGameLeaderboard update = MapstructUtils.convert(bo, RelaxGameLeaderboard.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }
    
    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(RelaxGameLeaderboard entity) {
        //TODO 做一些数据校验,如唯一约束
    }
    
    /**
     * 校验并批量删除游戏排行榜信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
